<template>
	<view>
		
	</view>
</template>

<script>
	import config from "@/config.js"
	import logStorage from "../../commom/logStorage";
	import { decryptPhone } from "@/services/phoneDecryption.js"
	export default {
		data() {
			return {
				isGZH:0
			}
		},
		onLoad(e) {
			console.log("load params" , e);
			logStorage.info("获取公众号的参数， " + JSON.stringify(e) )
			//获取公众号传入的二维码
			if(Object.keys(e).length>0){
				getApp().globalData.pubopenid = e.pubopenid;
				getApp().globalData.gradeId = e.gradeid;
				getApp().globalData.organizationId = e.organizationId;
				getApp().globalData.schoolId = e.schoolId;
				getApp().globalData.mId = e.mId;
			}
			
			//todo
			// pubopenid=oegDh6eZA4qazwIV5qRv3LZoUwZc&schoolId=508138db-94f0-4c14-64a7-08dd01fdbb28&gradeid=&mId=1692957676
			// gradeid=&mId=1614959094&pubopenid=oUjp76CTJke7Y_quX1t2hbR6AYkY&schoolId=41dbf6a3-026f-4255-1484-08da0a15f1d0
			// getApp().globalData.pubopenid = "oUjp76JCIt7OVPCrz7VVOZqGNHSE";
			// getApp().globalData.gradeId = "";
			// getApp().globalData.organizationId = "";
			// getApp().globalData.schoolId = "14dfd2e3-62b1-4607-9472-08dcda1082b5";
			// getApp().globalData.mId = "1614959094";
			//todo
			 uni.setStorageSync('tenantId', config.TENANT_ID )
			this.login()
		},
		methods: {
			
			login(){
				let that=this
				uni.login({
					success(res2) {
						that.getOpenId(res2.code)
					}
				})
			},
			getOpenId(code){
				this.$http('/pay/api/wechart/getSessionNew',{
					code:code,
					appId:getApp().globalData.appId
				},res3=>{
					uni.setStorageSync('openId',res3.data.openid)
					uni.setStorageSync('sk',res3.data.sk || res3.data.session_key)
					this.getLogin()
				},'GET')
			},
			getLogin(){
				this.$http('/minprogram/api/member/verify',{
					"wxOpenId":uni.getStorageSync('openId'),
					"pubopenid": getApp().globalData.pubopenid || ""
				},res4=>{
					uni.setStorageSync('token',res4.data.token)
					uni.setStorageSync("newUser", false)
					this.getMemberInfo();
				},'POST',()=>{
					uni.removeStorageSync('token')
					uni.setStorageSync("newUser", true)
					
					uni.switchTab({
						url: "/pages/tabbar/my"
					})
				})
			},
			getMemberInfo() {
				this.$http('/minprogram/api/member/getOwnerMemberCardList',{
					condition: {dataTypeFlag: ""}
				},async (res) => {
					if (res.data.items.length>0) {
						let list = res.data.items[0];
						let ownerName = await decryptPhone(list.ownerName,this)
						list.ownerName = ownerName
						if(list.limitCount != 0){
							this.setLimitCount(list.id)
						}
						uni.setStorageSync('tenantId', list.tenantId )
						uni.setStorageSync('info', list);
						uni.switchTab({
							url: "/pages/tabbar/my"
						})
					}
				})
			},
			setLimitCount(id){
				let data = {id,"limitCount":"0","limitPrice":0}
				this.$http('/common/api/k12/setLimitCount',data,res=>{})
			},
		}
	}
</script>

<style>

</style>
